//
//  YPLikeHelper.m
//  WHISPER
//
//  Created by lanou3g on 16/1/1.
//  Copyright © 2016年 王艳苹. All rights reserved.
//

#import "YPLikeHelper.h"
#import "AVObject.h"
#import "AVQuery.h"

@implementation YPLikeHelper

- (void)addLikeWithUrl:(NSString *)url name:(NSString *)name{
    AVObject *like = [AVObject objectWithClassName:@"user_like"];//表格
    //存储字段
    [like setObject:url forKey:@"likeUrl"];
    [like setObject:name forKey:@"userName"];
    [like saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"谢谢喜欢");
        }else{
            NSLog(@"失败");
        }
    }];
}
- (void)deleteLike:(NSString *)url name:(NSString *)name{
    AVQuery *query = [AVQuery queryWithClassName:@"user_like"];
    [query whereKey:@"likeUrl" equalTo:url];
    [query whereKey:@"userName" equalTo:name];
    //通过约束
    AVObject *like = [query getFirstObject];
    [like deleteInBackground];//从云端删除掉
}

- (void)isLikeWith:(NSString *)url name:(NSString *)name success:(void(^)(BOOL))success{
    AVQuery *query = [AVQuery queryWithClassName:@"user_like"];
    [query whereKey:@"likeUrl" equalTo:url];
    [query whereKey:@"userName" equalTo:name];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            if (objects.count == 0) {
                success(NO);
            }else{
                success(YES);
            }
        } else {
            // 输出错误信息
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}
@end
